<?php

namespace app\api\controller;

use app\BaseController;
use OSS\Result\DeleteObjectVersionsResult;
use think\facade\Db;

class Team extends BaseController
{
    /**
     * 列表
     */
    public function list()
    {
        $list = Db::name('team')->paginate(10)->toArray();

        if ($list) {
            foreach ($list['data'] as $key => $value) {
                $arge = Db::name('area')->whereIN('area_id',$value['city'])->find();
                $parent = Db::name('area')->whereIN('area_no',$arge['parent_no'])->find();
                $list['data'][$key]['city_name'] = $parent['area_name'].','.$arge['area_name'];

                // $list['data'][$key]['city_name'] = Db::name('area')->where('area_id',$value['city'])->value('area_name');
                $list['data'][$key]['work_name'] = Db::name('jobclass')->whereIN('id',$value['typework'])->field('id,name')->select();
                $list['data'][$key]['create_time'] = date("Y-m-d",$value['create_time']);
            }

            return json(['code'=>1,'data'=>$list]);
        } else {
            return json(['code'=>0,'info'=>'获取失败']);
        }
    }

    /**
     * 列表
     */
    public function get()
    {
        $input = $this->_vali([
            'id.require'          => 'id不能为空！',
        ],'GET');

        $data = Db::name('team')->where('id',$input['id'])->find();

        if ($data) {
            $data['create_time'] = date("Y-m-d",$data['create_time']);
            $data['work_name'] = Db::name('jobclass')->whereIN('id',$data['typework'])->field('id,name')->select();
            $arge = Db::name('area')->whereIN('area_id',$data['city'])->find();
            $parent = Db::name('area')->whereIN('area_no',$arge['parent_no'])->find();
            $data['city_name'] = $parent['area_name'].','.$arge['area_name'];

            $data['captain'] = Db::name('ranks')->alias('r')
                ->join('user u','r.inviter_id = u.id')
                ->where('team_id',$input['id'])
                ->where('type',2)
                ->field('u.id,u.username')
                ->select();
            if (!$data['captain']) {
                $data['captain'] = [];
            }

            $data['member'] = Db::name('ranks')->alias('r')
                ->join('user u','r.inviter_id = u.id')
                ->where('team_id',$input['id'])
                ->where('type',3)
                ->field('u.id,u.username')
                ->select();
            if (!$data['member']) {
                $data['member'] = [];
            }

            return json(['code'=>1,'data'=>$data]);
        } else {
            return json(['code'=>0,'info'=>'获取失败']);
        }
    }

    /**
     * 列表
     */
    public function detail()
    {
        $input = $this->_vali([
            'id.require'          => 'id不能为空！',
        ],'get');

        $data = Db::name('team')->where('id',$input['id'])->field('id,name,typework,city')->find();

        if ($data) {
            $data['work_name'] = Db::name('jobclass')->whereIN('id',$data['typework'])->field('id,name')->select();
            $arge = Db::name('area')->whereIN('area_id',$data['city'])->find();
            $parent = Db::name('area')->whereIN('area_no',$arge['parent_no'])->find();
            $data['province'] = $parent['area_name'];
            $data['city_name'] = $arge['area_name'];

            if ($data['typework']) {
                $data['typework'] = explode(',',$data['typework']);

                foreach ($data['typework'] as $key => $value) {
                    $data['typework'][$key] = (int) $value;
                }
            }


            return json(['code'=>1,'data'=>$data]);
        } else {
            return json(['code'=>0,'info'=>'获取失败']);
        }
    }

    /**
     * 发布职位
     */
    public function submit()
    {
        $data = $this->_vali([
            'name.require'          => '队伍名称不能为空！',
            'typework.require'      => '所属工种不能为空！',
            'city.require'          => '请选择地区',
        ],'POST');

        $data['uid'] = $this->request->users['id'];
        $data['create_time'] = time();
        $data['update_time'] = time();

        $data['city'] = Db::name('area')->where('area_name',$data['city'])->value('area_id');

        if ($data['typework']) {
            $data['typework'] = implode(",", $data['typework']);
        }

        $getid = Db::name('team')->insertGetId($data);

        if ($getid) {
             Db::name('ranks')->insert([
                'team_id' => $getid,
                'inviter_id' => $this->request->users['id'],
                'type' => 1,
                'create_time' => time(),
                'update_time' => time(),
                'status' => 1,
            ]);

            return json(['code'=>1,'info'=>'添加成功']);
        } else {
            return json(['code'=>0,'info'=>'添加失败']);
        }
    }

    /**
     * 发布职位
     */
    public function edit()
    {
        $data = $this->_vali([
            'id.require'          => 'id不能为空！',
            'name.require'          => '队伍名称不能为空！',
            'typework.require'      => '所属工种不能为空！',
            'city_name.require'          => '请选择地区',
        ],'POST');

        $data['city'] = Db::name('area')->where('area_name',$data['city_name'])->value('area_id');

        if ($data['typework']) {
            $data['typework'] = implode(",",$data['typework']);
        }

        unset($data['city_name']);

        $res = Db::name('team')->where('id',$data['id'])->update($data);

        if ($res) {
            return json(['code'=>1,'info'=>'编辑成功']);
        } else {
            return json(['code'=>0,'info'=>'编辑失败']);
        }
    }

    /**
     * 邀请
     */
    public function invite(){
        $data = $this->_vali([
            'phone.require'          => '队伍名称不能为空！',
            'team_id.require'          => '队伍id不能为空！',
            'type.require'          => '类型不能为空',
        ],'POST');

        $userid = Db::name('user')->where('phone',$data['phone'])->value('id');
        if (!$userid) {
            return json(['code'=>0,'info'=>'用户不存在']);
        }

        if ($userid == $this->request->users['id']) {
            return json(['code'=>0,'info'=>'不能邀请自己']);
        }

        $ranks = Db::name('ranks')->where('team_id',$data['team_id'])->where('inviter_id',$userid)->find();
        if ($ranks) {
            return json(['code'=>0,'info'=>'成员已存在']);
        }

        $res = Db::name('ranks')->insert([
            'team_id' => $data['team_id'],
            'inviter_id' => $userid,
            'status' => 0,
            'type' => $data['type'],
            'create_time' => time(),
            'update_time' => time(),
        ]);

        if ($res) {
            return json(['code'=>1,'info'=>'添加成功']);
        } else {
            return json(['code'=>0,'info'=>'添加失败']);
        }
    }
}